package cn.stylefeng.guns.modular.business.frequency.cmarea.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.cmarea.entity.CmArea;
import cn.stylefeng.guns.modular.business.frequency.cmarea.mapper.CmAreaMapper;
import cn.stylefeng.guns.modular.business.frequency.cmarea.model.param.CmAreaParam;
import cn.stylefeng.guns.modular.business.frequency.cmarea.service.CmAreaService;
import cn.stylefeng.guns.modular.business.frequency.cmarea.enums.CmAreaExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * cm_area 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class CmAreaServiceImpl extends ServiceImpl<CmAreaMapper, CmArea> implements CmAreaService {

    @Override
    public PageResult<CmArea> page(CmAreaParam cmAreaParam) {

        // 构造条件
        LambdaQueryWrapper<CmArea> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CmArea> list(CmAreaParam cmAreaParam) {

        // 构造条件
        LambdaQueryWrapper<CmArea> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(CmAreaParam cmAreaParam) {

        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(cmAreaParam, false);

        // 将dto转为实体
        CmArea cmArea = new CmArea();
        BeanUtil.copyProperties(cmAreaParam, cmArea);

        this.save(cmArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CmAreaParam cmAreaParam) {
        this.removeById(cmAreaParam.getCode());
    }

    @Override
    public void edit(CmAreaParam cmAreaParam) {

        // 根据id查询实体
        CmArea cmArea = this.queryCmArea(cmAreaParam);

        // 校验参数，检查是否存在重复的编码或者名称，排除当前编辑的这条记录
        checkParam(cmAreaParam, true);

        // 请求参数转化为实体
        BeanUtil.copyProperties(cmAreaParam, cmArea);

        this.updateById(cmArea);
    }

    @Override
    public CmArea detail(CmAreaParam cmAreaParam) {
        return this.queryCmArea(cmAreaParam);
    }


    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private void checkParam(CmAreaParam cmAreaParam, boolean isExcludeSelf) {
        Integer id = cmAreaParam.getCode();
        Integer code = cmAreaParam.getCode();

        // 构建带code的查询条件
        LambdaQueryWrapper<CmArea> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(CmArea::getCode, code);

        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(CmArea::getCode, id);
        }

        // 查询重复记录的数量
        int countByCode = this.count(queryWrapperByCode);

        // 如果存在重复的记录，抛出异常，直接返回前端
        if (countByCode >= 1) {
            throw new ServiceException(CmAreaExceptionEnum.CODE_REPEAT);
        }
    }

    /**
     * 获取cm_area
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private CmArea queryCmArea(CmAreaParam cmAreaParam) {
        CmArea cmArea = this.getById(cmAreaParam.getCode());
        if (ObjectUtil.isEmpty(cmArea)) {
            throw new ServiceException(CmAreaExceptionEnum.NOT_EXISTED);
        }
        return cmArea;
    }
}